package work.jame.blog.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import work.jame.blog.framework.VerificationGroup;
import work.jame.blog.framework.limit.RequestLimit;
import work.jame.blog.framework.result.PageResult;
import work.jame.blog.framework.result.Result;
import work.jame.blog.pojo.Comment;
import work.jame.blog.pojo.dto.CommentDTO;
import work.jame.blog.pojo.vo.SonCommentVO;
import work.jame.blog.service.CommentService;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jame
 * @since 2023-05-18
 */
@Api(tags = "评论")
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestLimit(value = 10,timeUnit = TimeUnit.MINUTES)
    @ApiOperation("发送评论")
    @PostMapping("/send")
    public Result<?> sendComment(@Validated(VerificationGroup.Save.class) @RequestBody CommentDTO commentDTO) {
        if (commentService.sendComment(commentDTO)) {
            return Result.succeedMessage("发送成功");
        }
        return Result.fail();
    }

    @RequestLimit(value = 10,timeUnit = TimeUnit.MINUTES)
    @ApiOperation("删除评论")
    @GetMapping("/delete")
    public Result<?> deleteComment(@RequestParam("commentId") Long commentId) {
        if (commentService.deleteComment(commentId)) {
            return Result.succeedMessage("删除评论成功");
        }
        return Result.fail("删除评论失败");
    }

    @RequestLimit(value = 40,timeUnit = TimeUnit.MINUTES)
    @ApiOperation("查询评论")
    @GetMapping("/queryComment")
    public PageResult<?> queryCommentInfo(@RequestParam("articleId") Long articleId,
                                          @RequestParam(
                                                  value = "pageNumber",
                                                  required = false,
                                                  defaultValue = "1"
                                          ) Long pageNumber,
                                          @RequestParam(
                                                  value = "orderType",
                                                  required = false,
                                                  defaultValue = "true"
                                          ) Boolean orderType) {
        return commentService.queryCommentByArticleId(articleId, pageNumber, 8L, orderType);
    }

    @RequestLimit(value = 40,timeUnit = TimeUnit.MINUTES)
    @ApiOperation("查询子级评论")
    @GetMapping("/querySonComment")
    public Result<?> querySonCommentInfo(@RequestParam("parentCommentId") Long parentCommentId,
                                         @RequestParam(
                                                 value = "pageNumber",
                                                 required = false,
                                                 defaultValue = "1"
                                         ) Long pageNumber) {
        List<SonCommentVO> list = commentService.querySonCommentLimit4(parentCommentId, pageNumber);
        if (Objects.isNull(list) || list.isEmpty()) {
            return Result.noData();
        }
        return Result.succeed(list);
    }


}
